package com.foreveredu.propms.web.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.BuildingType;
import com.foreveredu.propms.domain.ChargeCriterion;
import com.foreveredu.propms.domain.ChargeItem;
import com.foreveredu.propms.domain.HouseSource;
import com.foreveredu.propms.domain.HouseType;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.domain.ParkLocation;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.service.IChargeService;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.ChargeCriterionActionForm;

public class ChargeCriterionAction extends AbstractBaseDispatchAction {

	private IChargeService chargeService = null;
	private IManagementService managementService = null;
	
	public IChargeService getChargeService() {
		return chargeService;
	}
	
	public void setChargeService(IChargeService chargeService) {
		this.chargeService = chargeService;
	}

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}
	
	// 跳转到收费标准列表页面
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if (offset < 0) {
			return mapping.findForward("criterionList");
		}
		//获得普通收费标准列表
		PageModel pm = chargeService.findChargeCriterionsList(offset);
		request.setAttribute("pm", pm);
		
		return mapping.findForward("criterionList");
	}

	// 跳转到添加收费标准页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//获得小区列表
		List<SubDistrict> subDistrictList = managementService.findSubDistricts();
		request.setAttribute("subDistrictList", subDistrictList);
		
		//获得收费项目列表
		List<ChargeItem> chargeItemList = chargeService.findChargeItems();
		request.setAttribute("chargeItemList", chargeItemList);
		
		//获得房产类型列表
		List<HouseType> houseTypeList = managementService.findHouseTypes();
		request.setAttribute("houseTypeList", houseTypeList);
		
		//获得楼栋类型列表
		List<BuildingType> buildingTypeList = managementService.findBuildingTypes();
		request.setAttribute("buildingTypeList", buildingTypeList);
		
		//获得房产来源列表
		List<HouseSource> houseSourceList = managementService.findHouseSources();
		request.setAttribute("houseSourceList", houseSourceList);
		
		//获得停车位置列表
		List<ParkLocation> parkLocationList = chargeService.findParkLocations();
		request.setAttribute("parkLocationList", parkLocationList);
		
		return mapping.findForward("toAdd");
	}
	
	// 完成收费标准添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//获得表单数据
		ChargeCriterionActionForm ccaf = (ChargeCriterionActionForm)form;
		
		if (ccaf.getSubDistrictId() == 0) {
			request.setAttribute("subDistrictIdError", "请选择所属小区！");
			return mapping.findForward("toAddError");
		}
		if (ccaf.getChargeItemId() == 0) {
			request.setAttribute("chargeItemError", "请选择收费项目！");
			return mapping.findForward("toAddError");
		}
		if (ccaf.getHouseTypeId() == 0) {
			request.setAttribute("houseTypeError", "请选择房产类型！");
			return mapping.findForward("toAddError");
		}
		if (ccaf.getBuildingTypeId() == 0) {
			request.setAttribute("buildingTypeError", "请选择楼栋类型！");
			return mapping.findForward("toAddError");
		}
		if (ccaf.getHouseSourceId() == 0) {
			request.setAttribute("houseSourceError", "请选择房产来源！");
			return mapping.findForward("toAddError");
		}
		
		//获得所属小区
		SubDistrict subDistrict = managementService.findSubDistrictById(ccaf.getSubDistrictId());
		
		//获得收费项目
		ChargeItem chargeItem = chargeService.findChargeItemById(ccaf.getChargeItemId());
		
		//获得房产类型、楼栋类型、房产来源、
		HouseType houseType = managementService.findHouseTypeById(ccaf.getHouseTypeId());
		BuildingType buildingType = managementService.findBuildingTypeById(ccaf.getBuildingTypeId());
		HouseSource houseSource = managementService.findHouseSourceById(ccaf.getHouseSourceId());
		
		//如果收费项目是停车费，则需要设置停车位置，反之，则无需设置
		ParkLocation parkLocation = null;
		if(chargeItem != null && chargeItem.getName().equals("停车费")) {
			if (ccaf.getParkLocationId() == 0) {
				request.setAttribute("parkLocationError", "请选择停车位置！");
				return mapping.findForward("toAddError");
			}
			parkLocation = chargeService.findParkLocationById(ccaf.getParkLocationId());
		}
		
		//保存收费标准对象
		ChargeCriterion cri = new ChargeCriterion();
		
		cri.setChargeItem(chargeItem);
		cri.setSubDistrict(subDistrict);
		
		cri.setHouseType(houseType);
		cri.setBuildingType(buildingType);
		cri.setHouseSource(houseSource);
		cri.setParkLocation(parkLocation);
		
		cri.setAmount(ccaf.getAmount());
		cri.setType(0);//设置"普通收费标准"状态
		cri.setEnabled(1);//设置"有效"状态
		
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		cri.setCreateDate(date);//设置创建日期		
		
		chargeService.saveChargeCriterion(cri);
		
		return mapping.findForward("toCriterionList");
	}

	// 跳转到收费标准修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ChargeCriterionActionForm ccaf = (ChargeCriterionActionForm)form;
		ChargeCriterion criterion = chargeService.findChargeCriterionById(ccaf.getId());
		request.setAttribute("criterion", criterion);
		
		//获得小区列表
		List<SubDistrict> subDistrictList = managementService.findSubDistricts();
		request.setAttribute("subDistrictList", subDistrictList);
		
		//获得收费项目列表
		List<ChargeItem> chargeItemList = chargeService.findChargeItems();
		request.setAttribute("chargeItemList", chargeItemList);
		
		//获得房产类型列表
		List<HouseType> houseTypeList = managementService.findHouseTypes();
		request.setAttribute("houseTypeList", houseTypeList);
		
		//获得楼栋类型列表
		List<BuildingType> buildingTypeList = managementService.findBuildingTypes();
		request.setAttribute("buildingTypeList", buildingTypeList);
		
		//获得房产来源列表
		List<HouseSource> houseSourceList = managementService.findHouseSources();
		request.setAttribute("houseSourceList", houseSourceList);
		
		//获得停车位置列表
		List<ParkLocation> parkLocationList = chargeService.findParkLocations();
		request.setAttribute("parkLocationList", parkLocationList);
		
		return mapping.findForward("toUpdate");
	}

	// 保存收费标准修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得表单数据
		ChargeCriterionActionForm ccaf = (ChargeCriterionActionForm)form;
		
		if (ccaf.getSubDistrictId() == 0) {
			request.setAttribute("subDistrictIdError", "请选择所属小区！");
			return mapping.findForward("toAddError");
		}
		if (ccaf.getChargeItemId() == 0) {
			request.setAttribute("chargeItemError", "请选择收费项目！");
			return mapping.findForward("toAddError");
		}
		if (ccaf.getHouseTypeId() == 0) {
			request.setAttribute("houseTypeError", "请选择房产类型！");
			return mapping.findForward("toAddError");
		}
		if (ccaf.getBuildingTypeId() == 0) {
			request.setAttribute("buildingTypeError", "请选择楼栋类型！");
			return mapping.findForward("toAddError");
		}
		if (ccaf.getHouseSourceId() == 0) {
			request.setAttribute("houseSourceError", "请选择房产来源！");
			return mapping.findForward("toAddError");
		}

		
		//获得所属小区
		SubDistrict subDistrict = managementService.findSubDistrictById(ccaf.getSubDistrictId());
		
		//获得收费项目
		ChargeItem chargeItem = chargeService.findChargeItemById(ccaf.getChargeItemId());
		
		//获得房产类型、楼栋类型、房产来源、
		
		HouseType houseType = managementService.findHouseTypeById(ccaf.getHouseTypeId());
		BuildingType buildingType = managementService.findBuildingTypeById(ccaf.getBuildingTypeId());
		HouseSource houseSource = managementService.findHouseSourceById(ccaf.getHouseSourceId());
		
		//如果收费项目是停车费，则需要设置停车位置，反之，则无需设置
		ParkLocation parkLocation = null;
		if(chargeItem != null && chargeItem.getName().equals("停车费")) {
			if (ccaf.getParkLocationId() == 0) {
				request.setAttribute("parkLocationError", "请选择停车位置！");
				return mapping.findForward("toAddError");
			}
			parkLocation = chargeService.findParkLocationById(ccaf.getParkLocationId());
		}
		
		//保存收费标准对象
		ChargeCriterion cri = new ChargeCriterion();
		PropertyUtils.copyProperties(cri, ccaf);
		
		cri.setSubDistrict(subDistrict);
		cri.setHouseType(houseType);
		cri.setBuildingType(buildingType);
		cri.setHouseSource(houseSource);
		cri.setParkLocation(parkLocation);
		cri.setChargeItem(chargeItem);
		cri.setAmount(ccaf.getAmount());
		cri.setEnabled(1);//设置"有效"状态
		
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		cri.setCreateDate(date);//设置创建日期		
		
		chargeService.updateChargeCriterion(cri);

		return mapping.findForward("toCriterionList");
	}

	// 完成收费标准删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChargeCriterionActionForm ccaf = (ChargeCriterionActionForm)form;
		chargeService.deleteChargeCriterionById(ccaf.getId());
		return mapping.findForward("toCriterionList");
	}

}
